package com.firemoss.atonement.example.contactmanager.control
{
	import com.firemoss.atonement.example.contactmanager.control.event.EditorEvent;
	import com.firemoss.atonement.example.contactmanager.model.Contact;
	import com.firemoss.atonement.example.contactmanager.service.ContactService;
	import com.firemoss.atonement.example.contactmanager.view.ContactForm;
	import com.firemoss.atonement.example.contactmanager.view.model.ContactManagerPresentationModel;
	
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import org.swizframework.controller.AbstractController;
	
	public class ContactManagerController extends AbstractController
	{
		
		[Inject]
		public var service : ContactService
		
		[Inject]
		public var model : ContactManagerPresentationModel
		
		// UI MANAGEMENT
		
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.ContactEvent.OPEN_CONTACT_FOR_EDIT", properties="contact")]
		public function editContact( contact : Contact ) : void
		{
			var e : EditorEvent
			
			if ( !model.openContacts.contains( contact ) )
			{
				model.openContacts.addItem( contact )
					
				var form : ContactForm = new ContactForm()
				form.contact = contact
					
				e = new EditorEvent( EditorEvent.OPEN_EDITOR, form, contact )
			}
			else
			{
				e = new EditorEvent( EditorEvent.FOCUS_EDITOR, null, contact )
			}
			
			dispatcher.dispatchEvent( e );
		}
		
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.EditorEvent.CLOSE_EDITOR", properties="view")]
		public function editorClosed( view : Object ) : void
		{
			if ( view is ContactForm )
			{
				model.openContacts.removeItemAt( model.openContacts.getItemIndex( ContactForm( view ).contact ) );
			}
		}
		
		// DATA MANAGEMENT
		
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.ContactEvent.SAVE_CONTACT", properties="contact")]
		public function saveContact( contact : Contact) : void
		{
			service.saveContact( contact ).originalContact = contact
		}
		
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.ContactEvent.GET_CONTACT_COMPLETE", properties="contact")]
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.ContactEvent.SAVE_CONTACT_COMPLETE", properties="contact,originalContact")]
		public function contactReceived( contact : Contact, originalContact : Contact = null) : void
		{
			// Handle inserts
			if ( originalContact == null || contact != originalContact )
			{
				// Add to master list of contacts
				if ( !model.contacts.contains( contact ) )
				{
					model.contacts.addItem( contact )
				}
				
				// "Close" the original contact and know that we're editing the current
				if ( originalContact != null && model.openContacts.contains( originalContact ) )
				{
					model.openContacts.removeItemAt( model.openContacts.getItemIndex( originalContact ) )
					model.openContacts.addItem( contact )
				}
			}
			
		}
		
		[Mediate( event="mx.events.FlexEvent.APPLICATION_COMPLETE" )]
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.CompanyEvent.LIST_COMPANIES")]
		public function listCompanies() : void
		{
			service.listCompanies()
		}
		
		[Mediate( event="mx.events.FlexEvent.APPLICATION_COMPLETE" )]
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.ContactEvent.LIST_CONTACTS")]
		public function listContacts() : void
		{
			service.listContacts()
		}
		
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.CompanyEvent.LIST_COMPANIES_COMPLETE", properties="companies" )]
		public function updateCompanyList( companies : Array ) : void
		{
			model.companies.list = new ArrayCollection( companies )
				
			if ( !model.currentCompany && model.companies.length > 0)
			{
				model.currentCompany = model.companies[ 0 ];
			}
		}
		
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.ContactEvent.LIST_CONTACTS_COMPLETE", properties="contacts" )]
		public function updateContactList( contacts : Array ) : void
		{
			model.contacts.source = contacts
		}
		
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.CompanyEvent.LIST_COMPANIES_FAILED")]
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.CompanyEvent.SAVE_COMPANY_FAILED")]
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.ContactEvent.LIST_CONTACTS_FAILED")]
		[Mediate( event="com.firemoss.atonement.example.contactmanager.control.event.ContactEvent.SAVE_CONTACT_FAILED")]
		public function failure() : void
		{
			Alert.show( "We're sorry, but the server had an error." )
		}
		
	}
}